package com.ludomancy.gamelib;

public class Soundtrack
{
	private Music[] loops;
	private boolean loop = false;
	private int current = 0;
	
	public Soundtrack(String[] loopsToPlay)
	{
		loops = new Music[loopsToPlay.length];
		for (int i = 0; i < loopsToPlay.length; i++)
		{
			loops[i] = ResourceManager.getTrack(loopsToPlay[i]);
			loops[i].listener = this;
		}
	}
	
	public void play()
	{
		stop();
		current = 0;
		internalPlay();
	}
	
	private void internalPlay()
	{
		loops[current].play();
	}

	public void stop()
	{
		loops[current].stop();
	}

	public void update()
	{
	}

	public void loop()
	{
		loop = true;
		play();
	}

	public void onLoopOver(Music music)
	{
		current++;
		if(current == loops.length)
		{
			if(loop)
			{
				current = 0;
				internalPlay();
			}
			else
			{
				stop();
			}
		}
		else
		{
			internalPlay();
		}
	}
}
